home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / tlslite / integration / AsyncStateMachine.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  8.2 KB  |  268 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''
  5. A state machine for using TLS Lite with asynchronous I/O.
  6. '''
  7.  
  8. class AsyncStateMachine:
  9.     """
  10.     This is an abstract class that's used to integrate TLS Lite with
  11.     asyncore and Twisted.
  12.  
  13.     This class signals wantsReadsEvent() and wantsWriteEvent().  When
  14.     the underlying socket has become readable or writeable, the event
  15.     should be passed to this class by calling inReadEvent() or
  16.     inWriteEvent().  This class will then try to read or write through
  17.     the socket, and will update its state appropriately.
  18.  
  19.     This class will forward higher-level events to its subclass.  For
  20.     example, when a complete TLS record has been received,
  21.     outReadEvent() will be called with the decrypted data.
  22.     """
  23.     
  24.     def __init__(self):
  25.         self._clear()
  26.  
  27.     
  28.     def _clear(self):
  29.         self.handshaker = None
  30.         self.closer = None
  31.         self.reader = None
  32.         self.writer = None
  33.         self.result = None
  34.  
  35.     
  36.     def _checkAssert(self, maxActive = 1):
  37.         activeOps = 0
  38.         if self.handshaker:
  39.             activeOps += 1
  40.         
  41.         if self.closer:
  42.             activeOps += 1
  43.         
  44.         if self.reader:
  45.             activeOps += 1
  46.         
  47.         if self.writer:
  48.             activeOps += 1
  49.         
  50.         if self.result == None:
  51.             if activeOps != 0:
  52.                 raise AssertionError()
  53.             activeOps != 0
  54.         elif self.result in (0, 1):
  55.             if activeOps != 1:
  56.                 raise AssertionError()
  57.             activeOps != 1
  58.         else:
  59.             raise AssertionError()
  60.         if self.result == None > maxActive:
  61.             raise AssertionError()
  62.         self.result == None > maxActive
  63.  
  64.     
  65.     def wantsReadEvent(self):
  66.         '''If the state machine wants to read.
  67.  
  68.         If an operation is active, this returns whether or not the
  69.         operation wants to read from the socket.  If an operation is
  70.         not active, this returns None.
  71.  
  72.         @rtype: bool or None
  73.         @return: If the state machine wants to read.
  74.         '''
  75.         if self.result != None:
  76.             return self.result == 0
  77.  
  78.     
  79.     def wantsWriteEvent(self):
  80.         '''If the state machine wants to write.
  81.  
  82.         If an operation is active, this returns whether or not the
  83.         operation wants to write to the socket.  If an operation is
  84.         not active, this returns None.
  85.  
  86.         @rtype: bool or None
  87.         @return: If the state machine wants to write.
  88.         '''
  89.         if self.result != None:
  90.             return self.result == 1
  91.  
  92.     
  93.     def outConnectEvent(self):
  94.         '''Called when a handshake operation completes.
  95.  
  96.         May be overridden in subclass.
  97.         '''
  98.         pass
  99.  
  100.     
  101.     def outCloseEvent(self):
  102.         '''Called when a close operation completes.
  103.  
  104.         May be overridden in subclass.
  105.         '''
  106.         pass
  107.  
  108.     
  109.     def outReadEvent(self, readBuffer):
  110.         '''Called when a read operation completes.
  111.  
  112.         May be overridden in subclass.'''
  113.         pass
  114.  
  115.     
  116.     def outWriteEvent(self):
  117.         '''Called when a write operation completes.
  118.  
  119.         May be overridden in subclass.'''
  120.         pass
  121.  
  122.     
  123.     def inReadEvent(self):
  124.         '''Tell the state machine it can read from the socket.'''
  125.         
  126.         try:
  127.             self._checkAssert()
  128.             if self.handshaker:
  129.                 self._doHandshakeOp()
  130.             elif self.closer:
  131.                 self._doCloseOp()
  132.             elif self.reader:
  133.                 self._doReadOp()
  134.             elif self.writer:
  135.                 self._doWriteOp()
  136.             else:
  137.                 self.reader = self.tlsConnection.readAsync(16384)
  138.                 self._doReadOp()
  139.         except:
  140.             self._clear()
  141.             raise 
  142.  
  143.  
  144.     
  145.     def inWriteEvent(self):
  146.         '''Tell the state machine it can write to the socket.'''
  147.         
  148.         try:
  149.             self._checkAssert()
  150.             if self.handshaker:
  151.                 self._doHandshakeOp()
  152.             elif self.closer:
  153.                 self._doCloseOp()
  154.             elif self.reader:
  155.                 self._doReadOp()
  156.             elif self.writer:
  157.                 self._doWriteOp()
  158.             else:
  159.                 self.outWriteEvent()
  160.         except:
  161.             self._clear()
  162.             raise 
  163.  
  164.  
  165.     
  166.     def _doHandshakeOp(self):
  167.         
  168.         try:
  169.             self.result = self.handshaker.next()
  170.         except StopIteration:
  171.             self.handshaker = None
  172.             self.result = None
  173.             self.outConnectEvent()
  174.  
  175.  
  176.     
  177.     def _doCloseOp(self):
  178.         
  179.         try:
  180.             self.result = self.closer.next()
  181.         except StopIteration:
  182.             self.closer = None
  183.             self.result = None
  184.             self.outCloseEvent()
  185.  
  186.  
  187.     
  188.     def _doReadOp(self):
  189.         self.result = self.reader.next()
  190.         if self.result not in (0, 1):
  191.             readBuffer = self.result
  192.             self.reader = None
  193.             self.result = None
  194.             self.outReadEvent(readBuffer)
  195.         
  196.  
  197.     
  198.     def _doWriteOp(self):
  199.         
  200.         try:
  201.             self.result = self.writer.next()
  202.         except StopIteration:
  203.             self.writer = None
  204.             self.result = None
  205.  
  206.  
  207.     
  208.     def setHandshakeOp(self, handshaker):
  209.         '''Start a handshake operation.
  210.  
  211.         @type handshaker: generator
  212.         @param handshaker: A generator created by using one of the
  213.         asynchronous handshake functions (i.e. handshakeServerAsync, or
  214.         handshakeClientxxx(..., async=True).
  215.         '''
  216.         
  217.         try:
  218.             self._checkAssert(0)
  219.             self.handshaker = handshaker
  220.             self._doHandshakeOp()
  221.         except:
  222.             self._clear()
  223.             raise 
  224.  
  225.  
  226.     
  227.     def setServerHandshakeOp(self, **args):
  228.         '''Start a handshake operation.
  229.  
  230.         The arguments passed to this function will be forwarded to
  231.         L{tlslite.TLSConnection.TLSConnection.handshakeServerAsync}.
  232.         '''
  233.         handshaker = self.tlsConnection.handshakeServerAsync(**args)
  234.         self.setHandshakeOp(handshaker)
  235.  
  236.     
  237.     def setCloseOp(self):
  238.         '''Start a close operation.
  239.         '''
  240.         
  241.         try:
  242.             self._checkAssert(0)
  243.             self.closer = self.tlsConnection.closeAsync()
  244.             self._doCloseOp()
  245.         except:
  246.             self._clear()
  247.             raise 
  248.  
  249.  
  250.     
  251.     def setWriteOp(self, writeBuffer):
  252.         '''Start a write operation.
  253.  
  254.         @type writeBuffer: str
  255.         @param writeBuffer: The string to transmit.
  256.         '''
  257.         
  258.         try:
  259.             self._checkAssert(0)
  260.             self.writer = self.tlsConnection.writeAsync(writeBuffer)
  261.             self._doWriteOp()
  262.         except:
  263.             self._clear()
  264.             raise 
  265.  
  266.  
  267.  
  268.